package com.example.common.user.viewmodel;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import android.view.View;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.common.control.ICallback;
import com.example.common.model.CaptchaServiceGenerateResp;
import com.example.common.model.Description;
import com.example.common.component.LoginCtrlCom;
import com.example.common.model.SessionApiLoginResp;
import com.example.common.user.GlobalUser;

/**
 * 登陆页面ViewModel
 */
public class LoginViewModel extends ViewModel {
    private static final String TAG="LoginViewModel";

    private LoginCtrlCom loginCtrlCom;//登录网络控制组件

    private String phoneNumber;//手机号
    private String password;//密码
    private String captchaText;//验证码
    private Bitmap bitmap;//验证码图片
    private String Id;  //验证码Id
    private int status;//提示信息等级

    private MutableLiveData<Boolean> isGetCaptcha=new MutableLiveData<>();//获取验证码——状态
    private MutableLiveData<Boolean> isLogin=new MutableLiveData<>();//登录操作状态
    private MutableLiveData<Boolean> isActive=new MutableLiveData<>();//控制进度条状态
    private MutableLiveData<String> remainMsg=new MutableLiveData<>();//提示信息

    public LoginViewModel(){
        initData();
    }

    //数据预加载
    private void initData(){
        loginCtrlCom=new LoginCtrlCom();
        loadCaptcha();
    }

    /**
     * 点击获取验证码
     * @param view
     */
    public void onClickGetCaptcha(View view){
        //获取验证码
        loadCaptcha();
    }

    /**
     * 获取验证码
     */
    public void loadCaptcha(){
        loginCtrlCom.midmap(new ICallback() {
            @Override
            public void onSuccess(Object response) {
                //加载数据
                CaptchaServiceGenerateResp resp=(CaptchaServiceGenerateResp)response;
                byte[] decoded= Base64.decode(resp.getCaptcha().split(",")[1],Base64.DEFAULT);
                bitmap= BitmapFactory.decodeByteArray(decoded,0,decoded.length);
                Id=resp.getId();//保存Id
                if(isGetCaptcha.getValue()==null){
                    isGetCaptcha.setValue(true);
                }else {
                    isGetCaptcha.setValue(!isGetCaptcha.getValue());
                }
            }

            @Override
            public void onFailed(String mes) {
                remainMsg.setValue(mes);
            }
        });
    }

    /**
     * 校验验证码——通过后继续登录
     */
    private void checkCaptcha(){
        loginCtrlCom.checkCaptcha(Id, captchaText, new ICallback() {
            @Override
            public void onSuccess(Object response) {
                Log.v(TAG,"验证通过");
                login();//进行登录
                loadCaptcha();//重新加载验证码
            }

            @Override
            public void onFailed(String mes) {
                Log.v(TAG,"验证不通过");
                loadCaptcha();//重新加载验证码
                isLogin.setValue(false);
                isActive.setValue(false);
                status=3;
                remainMsg.setValue("验证码错误");
            }
        });
    }

    private void login(){
        loginCtrlCom.login(phoneNumber, password, new ICallback() {
            @Override
            public void onSuccess(Object response) {
                SessionApiLoginResp resp=(SessionApiLoginResp)response;

                Log.i(TAG, "onSuccess: 登录成功"+resp.getToken());

                //保存token-->view中对token进行刷新设置
                GlobalUser.TOKEN=resp.getToken();
                GlobalUser.ID=resp.getUser().getId();//设置ID
                GlobalUser.AVATAR_URL = resp.getUser().getAvatar();//头像
                GlobalUser.USERNAME = resp.getUser().getUsername();//用户名
                GlobalUser.getInstance().getBindUserName().setValue(GlobalUser.USERNAME);
                GlobalUser.getInstance().getBindAvatarUrl().setValue(GlobalUser.AVATAR_URL);

                isLogin.setValue(true);
                isActive.setValue(false);
            }

            @Override
            public void onFailed(String msg) {
                Log.i(TAG, "onFailed: 登录失败");
                isLogin.setValue(false);
                isActive.setValue(false);
                status=3;
                remainMsg.setValue(msg);
            }
        });
    }

    /**
     * 点击登陆
     * @param view
     */
    public void onClickLogin(View view){
        isActive.setValue(true);
        checkCaptcha();
    }

    public String getPassword() {
        return password;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public String getCaptchaText() {
        return captchaText;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void setCaptchaText(String captchaText) {
        this.captchaText = captchaText;
    }

    public MutableLiveData<Boolean> getIsGetCaptcha() {
        return isGetCaptcha;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }

    public MutableLiveData<Boolean> getIsLogin() {
        return isLogin;
    }

    public MutableLiveData<Boolean> getIsActive() {
        return isActive;
    }

    public int getStatus() {
        return status;
    }

    public MutableLiveData<String> getRemainMsg() {
        return remainMsg;
    }
}
